1. /* dbl2bdec.cpp by K.Tsuru */
  2. /*************************************************
  3. For |x| < BRADIX double x --> SDecimal array
  4. In "snum.h" FigBlock is defined by
  5. typedef NCBlock<fType> FigBlock;
  6. Associate function for SDouble and SDecimal classes
  7. **************************************************/
  8. #ifndef SN_H
  9. #include "sn.h"
  10. #endif
  11. int doubleToBinDec(double x, FigBlock& res, uint *size){
  12. if(res.size() == 0){
  13. *size = 0; return -2;
  14. }
  15. // x == 0.0 ?
  16. if(x == 0.0){
  17. res.clear();
  18. *size = 1u;
  19. return 0;
  20. }
  21. double dp = fabs(x);
  22. if(dp >= BRADIX) return -1;
  23. int e;
  24. frexp(dp, &e); // dp = y*2^e
  25. uint sz = (abs(e)+DOUBLE_BITS)/BRADIX_BITS + 2u;
  26. if( sz > res.size() ) sz = res.size();
  27. res.clear();
  28. uint j = 0;
  29. int f = 0;
  30. // can be converted exactly to binary
  31. while(dp < 1.0){
  32. dp *= BRADIX; j++;
  33. }
  34. for(; j < sz; j++){
  35. res[j] = (fType)dp;
  36. f++;
  37. dp -= (double)res[j];
  38. if(dp == 0.0) break;
  39. dp *= BRADIX;
  40. }
  41. *size = min(j+1u, sz);
  42. if(dp > 0.0) f = -2; // not enough figures
  43. return f;
  44. }

dbl2bdec.cpp : last modifiled at 2017/03/13 14:31:56(1,061 bytes)
created at 2016/04/11 11:17:20
The creation time of this html file is 2017/10/07 10:54:15 (Sat Oct 07 10:54:15 2017).